package com.cmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmj.Utils.BeanCopyUtils;
import com.cmj.dao.TagMapper;
import com.cmj.domain.ResponseResult;
import com.cmj.domain.SystemConstants;
import com.cmj.domain.dto.TagListDto;
import com.cmj.domain.entity.Tag;
import com.cmj.domain.vo.PageVo;
import com.cmj.domain.vo.TagVo;
import com.cmj.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-07-19 22:33:38
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        //hasText作用是看是否为空，为空就是false，后面得Tag的两个条件就不会加入到筛选中
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());

        //分页实现
        Page<Tag> page = new Page<>();
        //页数
        page.setCurrent(pageNum);
        //页大小
        page.setSize(pageSize);
        page(page,queryWrapper);
        //封装返回
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(Tag tag) {
        int result = tagMapper.insert(tag);
        if (result == 0){
            throw new RuntimeException("插入失败，请稍后再试");
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delTag(Long[] id) {
        int result = tagMapper.deleteBatchIds(Arrays.asList(id));
        if (result == 0){
            throw new RuntimeException("删除错误，请稍后重试！");
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<TagVo> getTag(Long id) {
        Tag tag = tagMapper.selectById(id);
        if (isEmpty(tag)){
            throw new RuntimeException("查询此条目失败，请稍后重试！");
        }
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult getListAllTag() {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        List<Tag> tags = tagMapper.selectList(queryWrapper);
        if (isEmpty(tags)){
            new RuntimeException(SystemConstants.SELECT_FAIL);
        }
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tags, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }

    @Override
    public ResponseResult updateTag(Tag tag) {
        int result = tagMapper.updateById(tag);
        if (result == 0){
            throw new RuntimeException("更新错误，请稍后重试！");
        }
        return ResponseResult.okResult();
    }


}